{-------------------------------------------------------------------------------
  Author: Duo (iamduo@qq.com)
  DateTime: 2019/8/7 9:25:10

  TADOStoredProc
-------------------------------------------------------------------------------}
unit uDBData_ADOStoredProc;

interface

uses uDB_Base, uBizDBIntf, ADODB;

type
  TDBData_ADOStoredProc = class(TDBData_DataSet_Base, IDBStoredProc)
  private
    FADOStoredProc:TADOStoredProc;
  protected
    function DoGetParamsIntf:IDBParams;override;
  public
    constructor Create(const AOwnerIntf:IDBConn; const AConn:TADOConnection);
    destructor Destroy;override;
    { IDBStoredProc }
    function Exec:Integer;
    function GetStoredProcName:WideString;
    procedure SetStoredProcName(const AValue:WideString);
    property StoredProcName:WideString read GetStoredProcName write SetStoredProcName;
  end;

implementation

uses uDBParam_Parameters;

{ TDBData_ADOStoredProc }

constructor TDBData_ADOStoredProc.Create(const AOwnerIntf: IDBConn;
  const AConn: TADOConnection);
begin
  FADOStoredProc := TADOStoredProc.Create(nil);
  inherited Create(AOwnerIntf, FADOStoredProc);
  FADOStoredProc.Connection := AConn;
end;

destructor TDBData_ADOStoredProc.Destroy;
begin
  FADOStoredProc.Free;
  inherited;
end;

function TDBData_ADOStoredProc.DoGetParamsIntf: IDBParams;
begin
  Result := TDBParam_Parameters.Create(Self, FADOStoredProc.Parameters) as IDBParams;
end;

function TDBData_ADOStoredProc.Exec: Integer;
begin
  Lock;
  try
    FADOStoredProc.ExecProc;
    Result := FADOStoredProc.RecordCount;
  finally
    UnLock;
  end;
end;

function TDBData_ADOStoredProc.GetStoredProcName: WideString;
begin
  Lock;
  try
    Result := FADOStoredProc.ProcedureName;
  finally
    UnLock;
  end;
end;

procedure TDBData_ADOStoredProc.SetStoredProcName(const AValue: WideString);
begin
  Lock;
  try
    if FADOStoredProc.ProcedureName <> AValue then
    begin
      FADOStoredProc.ProcedureName := AValue;
      FADOStoredProc.Parameters.Refresh;
      FADOStoredProc.Prepared := true;
    end;
  finally
    UnLock;
  end;
end;

end.

